package cn.fsd.controller;

import cn.fsd.domain.Storeroom_commodity;
import cn.fsd.query.delivery.StoreroomCommodityQuery;
import cn.fsd.service.impl.StoreRoomCommodityServiceImpl;
import cn.fsd.vo.ResultBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/storeroom-commodity")
public class StoreRoomCommodityController {

  //TODO 需要将这个部分的controller，service以及mapper迁移到storeroom管理系统中然后使用openfeign调用

  @Autowired
  StoreRoomCommodityServiceImpl storeRoomCommodityService;

  @PostMapping("/page")
  @ApiOperation(value = "分页查询库房中存储的所有商品")
  public ResultBean getCommodityByPage(@RequestBody StoreroomCommodityQuery query) {
    if (StringUtils.isEmpty(query.getWarehouse_id())) {
      return ResultBean.error("库房id不能为空");
    }
    return ResultBean.success(storeRoomCommodityService.getCommodityByPage(query));
  }

  @PostMapping("/updateStoreValues")
  @ApiOperation(value = "修改库房存放商品的最高值和预警值")
  public ResultBean updateStoreValues(@RequestBody Storeroom_commodity storeroomCommodity) {
    if (StringUtils.isEmpty(storeroomCommodity.getStoreroom_id())) {
      return ResultBean.error("库房id不能为空");
    } else if (StringUtils.isEmpty(storeroomCommodity.getCommodity_id())) {
      return ResultBean.error("商品id不能为空");
    } else if (storeroomCommodity.getMax_num() == null || storeroomCommodity.getLow_num() == null) {
      return ResultBean.error("最高值和预警值不能为空");
    } else if (storeroomCommodity.getMax_num() < storeroomCommodity.getLow_num()) {
      return ResultBean.error("库存最高值不能小于预警值");
    }
    int r =  storeRoomCommodityService.updateStoreValues(storeroomCommodity);
    if(r==-1){
      return ResultBean.error("商品未找到");
    }else if(r==-2){
      return ResultBean.error("库存最高值不能低于库存量");
    }
    return ResultBean.success();
  }

  @PostMapping("/queryInventory")
  @ApiOperation(value = "查询商品库存量")
  public ResultBean queryInventory(@RequestBody StoreroomCommodityQuery query) {
    if (StringUtils.isEmpty(query.getWarehouse_id())) {
      return ResultBean.error("库房id不能为空");
    }
    return ResultBean.success(storeRoomCommodityService.queryInventory(query));
  }

  @PostMapping("/outOfStock")
  @ApiOperation(value = "缺货检查，查询库房商品缺货情况")
  public ResultBean queryOutOfStock(@RequestBody StoreroomCommodityQuery query) {
    if (StringUtils.isEmpty(query.getWarehouse_id())) {
      return ResultBean.error("库房id不能为空");
    }
    return ResultBean.success(storeRoomCommodityService.queryOutOfStock(query));
  }

  @PostMapping("/getById")
  @ApiOperation(value = "根据商品id获取商品进货相关信息（名称，代码，分类，单位，缺货数量）")
  public ResultBean getCommodityById(@RequestBody StoreroomCommodityQuery query) {
    return ResultBean.success(storeRoomCommodityService.getCommodityById(query.getCommodity_id(),
        query.getWarehouse_id()));
  }

  @PutMapping("/purchase")
  @ApiOperation(value = "修改进货量")
  public ResultBean updatePurchase(@RequestBody StoreroomCommodityQuery query){
    if(StringUtils.isEmpty(query.getWarehouse_id())){
      return ResultBean.error("库房id不能为空");
    }
    if(StringUtils.isEmpty(query.getCommodity_id())){
      return ResultBean.error("商品id不能为空");
    }
    if(storeRoomCommodityService.updatePurchase(query)==-1){
      return ResultBean.error("进货数量过多");
    }
    return ResultBean.success();
  }

  @PostMapping("/commodity")
  @ApiOperation("根据库房id和商品id获取库房商品信息")
  public ResultBean getCommodity(@RequestBody Storeroom_commodity storeroomCommodity){
    if(StringUtils.isEmpty(storeroomCommodity.getStoreroom_id())){
      return ResultBean.error("库房id不能为空");
    }
    if(StringUtils.isEmpty(storeroomCommodity.getCommodity_id())){
      return ResultBean.error("商品id不能为空");
    }
    return ResultBean.success(storeRoomCommodityService.getCommodity(storeroomCommodity.getStoreroom_id(),storeroomCommodity.getCommodity_id()));
  }

}
